package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.Showcase;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改展柜Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/11 9:14
 * @Version: v1.0.0
 */
public class UpdateShowcaseActivity extends AppCompatActivity implements ISimpleDialogListener {

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_name)
    AppCompatEditText mName;

    @BindView(R.id.et_address)
    AppCompatEditText mAddress;

    private static Showcase showcase;
    private static final int REQUEST_SIMPLE_DIALOG = 2;

    private static CommonDaoUtils<Showcase> showcaseDaoUtils = DaoUtilsStore.getInstance().getShowcaseDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_update_showcase);

        ButterKnife.bind(this);
        String str = getIntent().getStringExtra("showcase");
        showcase = FastJsonUtils.strToBean(str, Showcase.class);

        initView();
        initData();
    }

    public void initView() {
        mTitle.setLeftFinish(this);
        mTitle.setTitle("修改展柜");
    }

    private void initData() {
        if (null != showcase) {
            mName.setText(showcase.getName());
            mAddress.setText(String.valueOf(showcase.getAddress()));
        }
    }

    private void updateShowcase() {
        String name = mName.getText().toString();
        String address = mAddress.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ToastUtils.warning("展柜名称不能为空！");
            return;
        }

        if (TextUtils.isEmpty(address)) {
            ToastUtils.warning("地址不能为空！");
            return;
        }

        if (Integer.valueOf(address) < 1) {
            ToastUtils.warning("地址范围为1到255");
            return;
        }

        if (Integer.valueOf(address) >= 256) {
            ToastUtils.warning("地址范围为1到255");
            return;
        }

        showcase.setName(name);
        showcase.setAddress(Integer.valueOf(address));

        boolean flag = showcaseDaoUtils.update(showcase);
        if (flag) {
            ToastUtils.success("展柜信息修改成功");
            finish();
        } else {
            ToastUtils.error("展柜信息修改失败");
        }
    }

    @OnClick({R.id.btn_add_showcase})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add_showcase:
                KeyboardUtils.hideSoftInput(UpdateShowcaseActivity.this);
                SimpleDialogFragment.createBuilder(UpdateShowcaseActivity.this, getSupportFragmentManager())
                        .setTitle("系统提示")
                        .setMessage("您确定要修改展柜信息吗？")
                        .setPositiveButtonText("确定")
                        .setNegativeButtonText("取消")
                        .setCancelable(false)
                        .setRequestCode(REQUEST_SIMPLE_DIALOG)
                        .show();
                break;
            default:

                break;
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            updateShowcase();
        }
    }

}